﻿using System;
using System.Windows.Forms;

// Support for OracleClient deprecated in .Net 4. Using reflection instead.
using VNPT;
using VNPT.Data;
namespace VNPT.Data
{
    public partial class fOracleConnect : DevExpress.XtraEditors.XtraForm
    {
        public fOracleConnect()
        {
            InitializeComponent();
            this.Shown += (a, e) =>
            {
                LoadData();
            };
        }
        private void btnChapNhan_Click(object sender, EventArgs e)
        {
            Save();
            this.Close();
        }
        VNPT.Utility.Profile.Xml xml = new Utility.Profile.Xml();

        private void LoadData()
        {

            txtHOST.EditValue = DataSetting.HOST;
            spinPort.EditValue = DataSetting.PORT;
            txtSERVICE_NAME.EditValue = DataSetting.SERVICE_NAME;
            txtUID.EditValue = DataSetting.UID;
            txtPass.EditValue = DataSetting.PWD;
            
        }
        
        public string GetConnectionString()
        {
            this.ShowDialog();
            return DataSetting.CONNECTSTRING;
        }


        private void Save()
        {

            if (CheckConnect())
            {
                DataSetting.HOST = txtHOST.EditValue.ToString();
                DataSetting.PORT = int.Parse("0"+spinPort.EditValue);

                DataSetting.SERVICE_NAME = txtSERVICE_NAME.EditValue.ToString();

                DataSetting.UID =txtUID.EditValue.ToString();
                DataSetting.PWD = txtPass.EditValue.ToString();
            }
        }
        string strCnn = "SERVER=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={0})(PORT={1}))(CONNECT_DATA=(SERVICE_NAME={2})));uid={3};pwd={4};";
        private bool CheckConnect()
        {
            var cnn = string.Format(strCnn, txtHOST.EditValue, spinPort.EditValue, txtSERVICE_NAME.EditValue, txtUID.EditValue, txtPass.EditValue);
            DbNetData Db = new DbNetData(cnn, DataProvider.OracleClient);
            try
            {
                Db.Open();
                return true;
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Không thể kết nối!\n" + ex.Message, "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

        }
        private void btnKiemTra_Click(object sender, EventArgs e)
        {
            if (CheckConnect())
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Đã kết nối thành công!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}

